package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")  //绑定一个前缀，这样就可以在properties配置文件配置该类的属性
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117619951";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCQy8fJnhL+LH8RpCI6qqhKkQAodQy6AWVYSTQujTuNgWskthlqQ0WdpIgElxTqFJKmpy+xzPveZAh9fP4gBO0Ipqq0XrQepwm4/08Lec+4WwiICJ66PVTkx0oWpZfJVc3mpXpgqYm/dlHFMD9vUDGFs8529r62iawv/HLL/ffGM5YbynHfanKbwDD9lZriuKQwvrht2HSpZcJMMk0wsiunILYBAnOsO2eBb4uY97ClANRzyVgPH0d/xdI+UVx4XMVMyhQmdFcsAOpZt+xeTnZosILrIbRRq94EIO16TZrrI/aVRCeL8cU7d5yhd4P0dKh/0fhTMh3xEY5LMiyYgsK7AgMBAAECggEANOkCHH6S2iZQWJXtnBp4XKcXs7Dx8ZZI/jXhx5D8Tf0YtdWE+/uRGMXinirHDbJu1E8LcqFA+Eb9gz2FbxXdt+ePQVHJ59mKB/vjWoIskWVSfMMJnWyD9QWN4TCpgnzEPKADVAcfusL1pmS3CWn4bq8Bpx8LGZC9Rtus5zXqsJXfHscH+qKXHfFU/OvAcwrzMj0wb1pt636kPXyw+Y7Gba/XsvVMDzmQ3EPlTJ12OwKHectj7jksgOVE0adns7gImBVNqbGqDaGOhCMbafIU+hBsvvsVHvTvEmgIcJTkiErjrUlUn87cBJth0Z8uh05ciIppNu4YsmZiSq1WWH5ZIQKBgQD9UCXbB4cOUFvKNuPxj+UdiIIRSoj6Okp+NYdF+C5uzcF4jZxrOPIYFp5RkYXk0qwTZvTm8pSMI/hgDoA2GIUPFXGtzXGDH06zSPRDtIYKTpvuRovo7i1CNh3BIURSKq7yPJsC78QkTbeCIQIhQ2iDJrKgOHfUzPJdEtAHe+GTjwKBgQCSVPZ8kj+ly8sp3ODPb1poXvF/Bh40tzxHbCJM5PcwH478xM04w0WL5vCCgCrT+8yq2sVUUTWnifawwVMUuWIpwCeisRo4acmPY1uhVxIFxvvr1eNxvksazpPtA8KMYmSOEntewCTzSfImFC5nSHLbAF2KShzDrxl0gxZ+KO/YFQKBgElNqTbB7eZJgNZ6nenofx/VwfXpAMyQk8AEE3o/AIjWB/ev2yT4HK611Oi72OhHwr32nEY8ypnnStfZOmbtH4wm43etLP0J0Ud2hCEXm3ZeGyePyKsGSZiQennJ+7BfY7L7sMvfwH3U85a3oJspm5PgXe6ZiKvPBw/CAus3gSanAoGAUW5CdHQWQzczaapm2EiJMQeUsDFBYeiN/NjqBYxtGjSVp4gFBA92hjIPqS+Vpb272WBA63j9uKwHwbjzeNbpD37FD74qbzY/WjHabeA8QUbILicqmMNinBpVK1IqU2D1wDH8wW9/oHuH6zTT1dn1QHXll+1CMIsiSTohrDcvsTkCgYB46rShO2Y7u4H8y0Lbxl534JD8tbEDjgnixqP8zq7IyDpLCOfUnZ9uhPDZBldcvMmbydH70l5qQsQI+Ur00LaMCpNIR3ZZZ2O8lIUw5jLC+OQY5cjECjhWkxL/ZW2hXzCcK+XEWu1edxDScqZncd6ZZwz5ou7FGzkouNelrnZ45g==";    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjkYV3D5Vfvf230LUJ894uYGH6YBpIPdVyYVEAaXTWDfOUzDFaQdM+9GL+Ijh+czen4/C/gNAFJ71200jvCTe4J23RsuUzMGOql46eJYJeKGEgnkLD7VKuXtXqeIShNpA34Y3mAORsWm1vQ/mW/JDs/HUk2Pz+MJHD5slj+hf0XQLNFa0M2ybZ+45tXAOFNVqOmQdphHolTCK888SbxsmO8H7iEnNI1TYD6AFx7vV15nXXMYzNhHRJo5ozFocw4q/6S4G4dAR3Bd7ojIwzZ+tF2ti6h0ytQuHPbaAyMNTh73A3ya8QxKl9TB2kvvqyLiAbP1nacQSVOWsutXCwkE3rwIDAQAB";    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://lazzw1g3iw.52http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        //付款超时时间
        String timeout="1m";

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
